package com.fz.us.admin.company.service.impl;

import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.base.utils.StringUtils;
import com.fz.us.admin.company.bean.EnumManage;
import com.fz.us.admin.company.dao.PushEntityDao;
import com.fz.us.admin.company.entity.Active;
import com.fz.us.admin.company.entity.PushEntity;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.mp.WxEchoMpServiceImpl;
import com.fz.us.admin.company.service.PushEntityService;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.admin.company.service.jms.AdvancedNotifyMessageProducer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service实现类 - 推送
 * ============================================================================
 * 版权所有 2015 。
 *
 * @author fallenpanda
 *
 * @version 0.1 2015-01-21
 * ============================================================================
 */

@Service
public class PushEntityServiceImpl extends BaseServiceImpl<PushEntity, String> implements PushEntityService {

	@Resource
	private PushEntityDao pushEntityDao;
    @Resource
    private UserService userService;

    @Override
    public BaseDao<PushEntity, String> getBaseDao() {
        return pushEntityDao;
    }


    @Resource
    private AdvancedNotifyMessageProducer notifyMessageProducer;
    @Resource
    private WxEchoMpServiceImpl wxMpService;

    private static final int MAX_CONTENT_LENGTH = 30;

    @Override
    public PushEntity createPushEntity(Active active) {

        if(active.getReceiver()==null) return null;

        PushEntity pushEntity = new PushEntity();

        pushEntity.setPushLogId(active.getId());

        if(active.getSender()!=null){
            pushEntity.setSenderId(active.getSender().getId());
            pushEntity.setSenderName(active.getSender().getName());
        }

        pushEntity.setReceiverId(active.getReceiver());
//        pushEntity.setReceiverName(active.getReceiver().getName());

        pushEntity.setAppTarget(EnumManage.PushTargetEnum.Cst);
        pushEntity.setPushWay(EnumManage.PushWayEnum.Notification);

        pushEntity.setPushType(EnumManage.PushTypeEnum.Alias);
        pushEntity.setPushAlias(active.getReceiver());

        String pushTitle = "";//标题
        String pushContent = "";//内容

        EnumManage.CustomMessageLevelEnum msgLevel = null;
        EnumManage.CustomMessageTypeEnum msgType = null;
        String msgKey = "";
        String msgContent = "";

        Object target = null;//对象
        String targerKey=null;

        if (StringUtils.isNotEmpty(active.getTargetId())&& StringUtils.isNotEmpty(active.getTargetClass())) {
            target = getObject(active.getTargetClass(), active.getTargetId());
            String targetClass=active.getTargetClass();
            int index=targetClass.lastIndexOf(".");
            targerKey=targetClass.substring(index+1,targetClass.length());
        }

        if (EnumManage.ActiveTypeEnum.AdminMsg.equals(active.getType())) {//系统消息
            pushTitle = "系统消息";
            pushContent = active.getObjectText();

            msgLevel = EnumManage.CustomMessageLevelEnum.Normal;
            msgType = EnumManage.CustomMessageTypeEnum.AdminMsg;
            msgKey = "";
            msgContent = "";
        } else if (EnumManage.ActiveTypeEnum.CompanyMsg.equals(active.getType())) {//企业消息

        } else if (EnumManage.ActiveTypeEnum.UserMsg.equals(active.getType())) {//用户消息
            pushTitle = "用户消息";
            pushContent = active.getObjectText();

            msgLevel = EnumManage.CustomMessageLevelEnum.Normal;
            msgType = EnumManage.CustomMessageTypeEnum.UserMsg;
            msgKey = "";
            msgContent = "";
        } else if (EnumManage.ActiveTypeEnum.Resume.equals(active.getType())) {//履历动态

        } else if (EnumManage.ActiveTypeEnum.Comment.equals(active.getType())) {//批注动态
            pushTitle = "批注动态";

            String objectText = "";
            if(active.getObjectModel()!=null){
                if(EnumManage.CommentModelEnum.text.equals(active.getObjectModel())){
                    objectText = active.getObjectText();
                }else if(EnumManage.CommentModelEnum.image.equals(active.getObjectModel())){
                    objectText = "#图片#";
                }else if(EnumManage.CommentModelEnum.voice.equals(active.getObjectModel())){
                    objectText = "#语音#";
                }else if(EnumManage.CommentModelEnum.video.equals(active.getObjectModel())){
                    objectText = "#视频#";
                }else if(EnumManage.CommentModelEnum.location.equals(active.getObjectModel())){
                    objectText = "#位置#";
                }else if(EnumManage.CommentModelEnum.link.equals(active.getObjectModel())){
                    objectText = "#链接#";
                }else if(EnumManage.CommentModelEnum.file.equals(active.getObjectModel())){
                    objectText = "#文件#";
                }
            }else{
                objectText = active.getObjectText();
            }

            pushContent = active.getSender()!=null?active.getSender().getName()+"："+objectText:objectText;

            msgLevel = EnumManage.CustomMessageLevelEnum.Normal;
            msgType = EnumManage.CustomMessageTypeEnum.FormRead;
            msgKey =targerKey;
            msgContent = active.getTargetId();
        } else if (EnumManage.ActiveTypeEnum.Remind.equals(active.getType())) {//提醒动态
            pushTitle = "提醒动态";
            pushContent = active.getObjectText();

            msgLevel = EnumManage.CustomMessageLevelEnum.Normal;
            msgType = EnumManage.CustomMessageTypeEnum.AdminMsg;
            msgKey = "";
            msgContent = "";

        } else if (EnumManage.ActiveTypeEnum.Approval.equals(active.getType())) {//审批动态
            pushTitle = "审批动态";
            pushContent = active.getSender()!=null?active.getSender().getName()+"："+active.getObjectText():active.getObjectText();

            msgLevel = EnumManage.CustomMessageLevelEnum.Normal;
            msgType = EnumManage.CustomMessageTypeEnum.AdminMsg;
            msgKey = "";
            msgContent = "";
        }

        pushEntity.setPushTitle(pushTitle);
        if(pushContent.length() > MAX_CONTENT_LENGTH)
            pushContent = pushContent.substring(0, MAX_CONTENT_LENGTH) + "...";
        pushEntity.setPushContent(pushContent);

        pushEntity.setMsgLevel(msgLevel);
        pushEntity.setMsgType(msgType);
        pushEntity.setMsgKey(msgKey);
        pushEntity.setMsgContent(msgContent);

        return pushEntity;
    }

    @Override
    public void sendPushEntity(Active active) {
        PushEntity pushEntity = createPushEntity(active);
        //已发送对象
        List<User> toUsersList = new ArrayList<User>();
        if (pushEntity != null) {
            //极光推送
            notifyMessageProducer.sendQueue(pushEntity, EnumManage.NotifyKeyEnum.pushLog.name());
            if (EnumManage.CustomMessageTypeEnum.FormRead.equals(pushEntity.getMsgType()) || EnumManage.CustomMessageTypeEnum.AdminMsg.equals(pushEntity.getMsgType())) {
                //微信推送
                try {
                    if(StringUtils.isNotEmpty(active.getReceiver())){
                        for(String userId:active.getReceiver().split(",")){
                            User recerver= userService.get(userId);
                            if(!toUsersList.contains(recerver)){
                                wxMpService.sendFormReadMessage(recerver ,pushEntity.getPushContent(), pushEntity.getMsgKey(), pushEntity.getMsgContent());
                                toUsersList.add(recerver);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}